<?php

use Biz\model\activity\CouponConfig as mCouponConfig;
use Biz\model\salesRebate\SalesAccount as mSalesAccount;
use Biz\model\salesRebate\SalesCouponMap as mSalesCouponMap;

class GetCouponListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    const PAGE_SIZE = 30;

    public function main()
    {
        $uid = $this->uid;
        $page = $this->getRequest()->getPost('page', '');
        $page = $page ? $page : 1;
        $where = [
            'uid' => $uid,
            'coupon_type' => mSalesAccount::TYPE_APP,
            'coupon_status' => mSalesCouponMap::STATUS_NORMAL,
            'ORDER' => ['addtime' => 'DESC'],
            'LIMIT' => [($page - 1) * self::PAGE_SIZE, self::PAGE_SIZE],
        ];
        $couponIds = mSalesCouponMap::selectByWhere($where, 'coupon_id');
        unset($where['ORDER']);
        unset($where['LIMIT']);
        $count = mSalesCouponMap::getCountWhere($where);
        $arr = mCouponConfig::select('*', ['coupon_id' => $couponIds]);
        $list = [];
        foreach ($arr as $row) {
            $list[] = [
                'coupon_id' => $row['coupon_id'],
                'coupon_code' => $row['coupon_code'],
                'start_time' => $row['start_time'],
                'stop_time' => $row['stop_time'],
                'type' => $row['type'],
                'full_amount' => $row['full_amount'],
                'detract_amount' => $row['detract_amount'],
                'discount_level' => $row['discount_level'],
                'received_count' => $row['received_count'],
                'used_count' => $row['used_count']
            ];
        }
        $isOver = ($page * self::PAGE_SIZE >= $count) ? 1 : 0;
        $this->setSuccess(['list' => $list, 'is_over' => $isOver]);
    }

}